﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinnerCMS.CacheStorager
{
    /// <summary>
    /// memcached 存储器
    /// </summary>
    public class MemCachedStorager : ICacheStorager
    {
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        public void Add<T>(string key, T t)
        {
            Remove(key);
            if (TimeOut > 0)
            {
                MemCachedManager.CacheClient.Add(key, t, System.DateTime.Now.AddMinutes(TimeOut));
            }
            else
            {
                MemCachedManager.CacheClient.Add(key, t);
            }
        }
        public void Add<T>(string key, T value, int datetime)
        {
            Add<T>(key, value, Common.DateTimeType.Second, datetime);
        }
        public void Add<T>(string key, T value, Common.DateTimeType datetimetype, int datetime)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            return MemCachedManager.CacheClient.Get<string>(key);
        }
        /// <summary>
        /// 试图获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGet<T>(string key, out T value) where T : class
        {
            return MemCachedManager.CacheClient.TryGet<T>(key, null, false, out value);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            return MemCachedManager.CacheClient.Get<T>(key);
        }
        /// <summary>
        /// 移除指定ID的对象
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            if (MemCachedManager.CacheClient.KeyExists(key))
                MemCachedManager.CacheClient.Delete(key);
        }


        /// <summary>
        /// 到期时间
        /// </summary>
        public int TimeOut { set; get; }
        /// <summary>
        /// 统计信息
        /// </summary>
        /// <returns></returns>
        public Hashtable Stats()
        {
            return MemCachedManager.CacheClient.Stats();
        }
        public void Shutdown()
        {
            MemCachedManager.Shutdown();
        }




        public void RemoveLike(string key)
        {
            MemCachedManager.CacheClient.RemoveLike(key);
        }
        public void FlushAll()
        {
            MemCachedManager.CacheClient.FlushAll();
        }
    }
}